﻿#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

#include <climits>
using namespace std;

const int N = 1010;
int n, V, v[N], w[N];
int dp[N][N];
int main()
{
	// 读⼊数据
	cin >> n >> V;
	for (int i = 1; i <= n; i++)
	{
		cin >> v[i] >> w[i];
	}

	// 解决第⼀问
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= V; j++) // 修改遍历顺序
		{
			dp[i][j] = dp[i - 1][j];
			if (j >= v[i])
				dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
		}
	}
	cout << dp[n][V] << endl;

	// 解决第⼆问
	memset(dp, 0, sizeof dp);
	for (int j = 1; j <= V; j++)
	{
		dp[0][j] = -1;
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= V; j++) // 修改遍历顺序
		{
			dp[i][j] = dp[i - 1][j];
			if (j >= v[i] && dp[i - 1][j - v[i]] != -1)
				dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
		}
	}
		

	cout << (dp[n][V] == -1 ? 0 : dp[n][V]) << endl;
	return 0;
}